package cn.controller;

import cn.basic.BaseController;
import cn.dao.EquipmentDAO;
import cn.factory.DAOFactory;
import cn.model.TEquipment;
import cn.model.VEquipmentClassRoom;
import cn.util.Expression;
import cn.util.Toolsutil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 设备信息数据交互
 * @author WZG
 * @version 2020-2-30
 */
@Controller
@RequestMapping(value="/equipment")
public class EquipmentController extends BaseController {


    @RequestMapping(value="/addequipment")
    public void AddEquipment(
            String equipname,
            String type,
            String price,
            Integer classroomid,
            String number,
            String uselife,
            String description,
            String note,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {

        TEquipment equipment = new TEquipment();
        equipment.setEquipname(equipname);
        equipment.setType(type);
        equipment.setCreatetime(Toolsutil.getdatetimes());
        equipment.setPrice(price);
        equipment.setClassroomid(classroomid);
        equipment.setNumber(number);
        equipment.setUselife(uselife);
        equipment.setState(1);
        equipment.setDescription(description);
        equipment.setNote(note);

        System.out.println(Toolsutil.getdatetimes());

        EquipmentDAO equipdao = DAOFactory.getEquipmentDAO();
        if (equipdao.addEquipment(equipment) > 0) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "添加成功";
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "添加失败";
        }
        closeout();
    }

    /*
     * 实现一个角色的删除
     * @param userid 用户名
     * @throws IOException
     */
    @RequestMapping(value="/deleteequipment")
    public void deleteEquipment(
            String equipid,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException{
        EquipmentDAO equipdao = DAOFactory.getEquipmentDAO();
        if (equipid !=null && !equipid.equals("")){
            equipdao.deleteEquipment(Integer.parseInt(equipid));
            dt.code = dt.FLAG_SUCC;
            dt.msg = "删除成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除失败 ";
        }
        closeout();
    }

    @RequestMapping(value="/modifyequipment")
    public void modifyEquipment(
            Integer equipid,
            String equipname,
            String type,
            String price,
            Integer classroomid,
            String number,
            Integer state,
            String uselife,
            String description,
            String note,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        EquipmentDAO equipdao = DAOFactory.getEquipmentDAO();
        TEquipment equipment = new TEquipment();
        equipment .setEquipid(equipid);
        equipment.setEquipname(equipname);
        equipment.setType(type);
        equipment.setPrice(price);
        equipment.setCreatetime(Toolsutil.getdatetimes());
        equipment.setClassroomid(classroomid);
        equipment.setNumber(number);
        equipment.setUselife(uselife);
        equipment.setState(state);
        equipment.setDescription(description);
        equipment.setNote(note);

        if (equipdao.modifyEquipment(equipment)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }
    @RequestMapping(value="/getequipmentlist")//映射路径,拦截post请求
    public void getAllEquipmentList(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        EquipmentDAO equipdao = DAOFactory.getEquipmentDAO();
        List list = equipdao.getAllEquipment();

        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + list.size() + "条记录";
            dt.data = list;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }

    @RequestMapping(value="/getequipmentByPage")//映射路径,拦截post请求
    public void getEquipmentList(
            String equipname,
            String type,
            String createtime,
            Integer state,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {
        EquipmentDAO equipdao = DAOFactory.getEquipmentDAO();
        Expression exp = new Expression();
        if(equipname != null && !equipname.equals("")){
            exp.andEqu("equipname", equipname, String.class);}
        if(type != null && !type.equals("")){
            exp.andEqu("type", type, String.class);}
        if (createtime !=null && !createtime.equals("")){
            exp.andEqu("createtime",createtime,String.class);
        }
        if (state!=null && !state.equals("")){
            exp.andEqu("state",state,Integer.class);
        }
        String wherecondition = exp.toString();
        List<VEquipmentClassRoom> list = equipdao.getEquipmentList(wherecondition,page, limit);
        int size = equipdao.getEquipmentAmount(wherecondition);
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
}
